package com.krius;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Create By IntelliJ IDEA
 *
 * @Author: jsonor
 * @Create-Time: 2017/9/14 11:29
 * @Description: C3P0连接池的使用
 */
public class C3P0 {

  public static void main(String[] args) {
    // 创建连接池的类
    // 系统自动会在classes目录下搜索c3p0.properties文件
    ComboPooledDataSource ds = new ComboPooledDataSource("itcast");

    // 获取链接
    try {
      Connection cn = ds.getConnection();
      PreparedStatement ps = cn.prepareStatement("select * from user");
      ps.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  private static void HarderEncode() {
    // 创建连接池的类
    ComboPooledDataSource ds = new ComboPooledDataSource();

    // 设置参数
    try {
      ds.setDriverClass("com.mysql.jdbc.Driver");
      ds.setJdbcUrl("jdbc:mysql:///test");
      ds.setUser("root");
      ds.setPassword("123456789");
    } catch (PropertyVetoException e) {
      e.printStackTrace();
    }

    // 获取链接
    try {
      Connection cn = ds.getConnection();
      PreparedStatement ps = cn.prepareStatement("select * from user");
      ps.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}
